$(() => {
    load();
});
const reLoad = () => {
    $('#exampleTable').bootstrapTable('refresh')
}

$('#searchState').change(function () {
    reLoad()
})

const EntryScore = async (item = null) => {
    console.log('=====>>录入分数>', item)
    //文件
    layer.open({
        type: 1,
        area: ['500px', '500px'],
        title: "导入一次加密号",
        shade: 0.6,
        shadeClose: false,
        maxmin: true,
        anim: 0,
        content: `
<form class="layui-form layui-form-pane" style="width: 100%;height: 100%;padding: 10px;" id="myForm">
    <div class="layui-form-item">
    <label class="layui-form-label" style="width:auto;border: 0;">上传加密信息</label>
    </div>
    <div class="layui-form-item" style="height: calc(100% - 150px);">
<div class="layui-upload-drag" style=" height: 100%; display: flex; justify-content: center;  align-items: center;
flex-direction: column;" id="ID-upload-demo-drag">
  <i class="layui-icon layui-icon-upload"></i>
  <div class="layui-icon-upload-title">点击上传，或将文件拖拽到此处</div>
  <div class="layui-hide" id="ID-upload-demo-preview">
     上传文件
     <input type="text" name="name" placeholder="" autocomplete="off" disabled>
  </div>
</div>
  </div>
  <div class="layui-form-item" style="display: flex;    justify-content: center;">
    <button class="layui-btn" type="button"  lay-submit lay-filter="layedit-code-yes">提交</button>
  </div>
</form>
                `,
        success: function (layero, index) {
            layui.use(['form', 'upload'], async function () {
                var form = layui.form;
                var upload = layui.upload;
                //上传
                upload.render({
                    elem: '#ID-upload-demo-drag',
                    url: "/importExcelDecryptNum1",
                    data: { field:$('#searchState').val()},
                    exts: 'xls',
                    done: function (res) {
                        console.log(res)
                        if (res.code != "0") {
                            layer.msg(res.msg)
                        } else {
                            $('#ID-upload-demo-preview').removeClass('layui-hide')
                                .find('input').attr('value', 'res.files.file');
                            $('.layui-icon-upload').hide()
                            $('.layui-icon-upload-title').hide()
                            layer.msg('上传成功');
                            layer.close(index)
                            reLoad()
                        }
                    }
                });
                form.on('submit(layedit-code-yes)', function (data) {
                    const mydata = data.field
                    console.log('提交数据', mydata.name, mydata)
                    //关闭
                    layer.close(index);
                });
            })


        }
    });



}


const derive = () => {
    console.log('=====>>导出>',)
    layer.confirm(`是否导出实操赛场一次加密信息？`, {
        btn1: function (index) {
            window.location.href =  `/downloadImportDecrypt?field=${$('#searchState').val()}&type=1`;
            layer.close(index)
        }
    });
}
const template = () => {
    console.log("模板下载")
    window.location.href = '/exportListNum1'
}
const edit = (mydata) => {
    console.log('=====>>修改>', mydata)
    layer.open({
        type: 1,
        area: ['500px', '200px'],
        title: "修改",
        shade: 0.6,
        shadeClose: false,
        maxmin: true,
        anim: 0,
        content: `
<form class="layui-form layui-form-pane" style="width: 100%;height: 100%;padding: 10px;" id="myForm">
   <div class="layui-form-item">
    <label class="layui-form-label" style="width:auto;border: 0;"> '一次加密号'</label>
     <div class="layui-input-block">
      <input type="text" name="num1" value="${(mydata.num1 == null ? '' : mydata.num1)}" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item" style="display: flex;    justify-content: center;">
    <button class="layui-btn" type="button"  lay-submit lay-filter="layedit-code-yes">提交</button>
  </div>
</form>
                `,
        success: (layero, index) => {
            layui.use(['form'], () => {
                var form = layui.form;
                form.render('select');
                form.on('submit(layedit-code-yes)', (data) => {
                    console.log('提交数据', data.field, mydata)
                    let oldmydata=JSON.parse(JSON.stringify(mydata))
                    mydata=data.field;
                    mydata.id=oldmydata.id
                    mydata.field=$('#searchState').val()
                    console.log('提交数据', mydata)

                    //修改
                    $.ajax({
                        type: 'post',
                        data: JSON.stringify({
                            ...mydata
                        }),
                        contentType: 'application/json',
                        url: '/updateDecrypt',
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg('修改成功');
                                layer.close(index)
                                reLoad();
                            } else {
                                layer.msg(r.msg);
                            }
                        }
                    });


                });
            })


        }
    });



}

const remove = (id,num) => {
    layer.confirm("确认要删除选中的'" + num + "'条数据吗?", {
        btn: ['确定', '取消']
        // 按钮
    }, () => {
        console.log('=====>>删除>', id)
        $.ajax({
            type: 'get',
            data: {
                id,
                status:1
            },
            url: '/deltedDecrypt',
            success: function (r) {
                if (r.code == 0) {
                    layer.msg('删除成功');
                    reLoad();
                } else {
                    layer.msg(r.msg);
                }
            }
        });
    });

}

const columns =  [
    {
        field: 'num1',
        title: '一次加密号',
        width: '105px'
    },
    {
        title: '操作',
        field: 'id',
        align: 'center',
        // width:'50px',
        formatter: function (value, row, index) {
            return row.statuss==='1'?'':`          <a style="color: #18a689" class=" " href="#" mce_href="#" title="修改" onclick="edit(${JSON.stringify(row).replace(/\"/g, "'")})">修改</a>
                                     <a style="color: red" class=" " href="#" mce_href="#" title="删除" onclick="remove('${row.id}','${row.num1}')">删除</a>
                                    `;
        },
        width: '230px'
    }
]
columns.forEach(item => {
    item.align = 'center'
    item.valign='middle'
})

function load() {
    $('#exampleTable')
        .bootstrapTable(
            {
                method: 'post', // 服务器数据的请求方式 get or post
                url:'/getAddDecrypt', // 服务器数据的加载地址
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页，每页数据条数
                pageNumber: 1, // 如果设置了分布，首页页码
                //search : true, // 是否显示搜索框
                showColumns: false, // 是否显示内容下拉框（选择显示的列）
                sidePagination: "server", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                queryParamsType: "",
                queryParams: function (params) {
                    console.log('params', params)
                    return {
                        current: params.pageNumber,
                        size: params.pageSize,
                        field:$('#searchState').val(),
                        statuss:99,
                        status:1
                    };
                },
                responseHandler: function (res) {
                    if (res.code !== '0') {
                        layer.msg(res.msg);
                        return false
                    }
                    return {
                        "rows": res.data.records,
                        ...res.data
                    };
                },
                columns
            });
}